package com.yuantu.judiciary.controller;

import com.yuantu.judiciary.annotation.Log;
import com.yuantu.judiciary.common.Response;
import com.yuantu.judiciary.common.ResponseFactory;
import com.yuantu.judiciary.model.form.*;
import com.yuantu.judiciary.model.vo.*;
import com.yuantu.judiciary.service.IDiscountCodeService;
import com.yuantu.judiciary.service.IInviteCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 邀请码模块
 *
 * @author syw
 * @since 2023-08-17
 **/
@RestController
@RequestMapping("/discount-code")
@Api(tags = "推荐码模块")
@RequiredArgsConstructor
public class DiscountCodeController extends BaseController {


    private final IDiscountCodeService discountCodeService;

    //没有使用
//    @ApiOperation(value = "检测推荐码是否可用")
//    @PostMapping("/check/valid")
//    public Response<DiscountCodeCheckResultVO> checkDiscountValid(@RequestBody @Validated DiscountCodeCheckVO discountCodeCheck) {
//        return ResponseFactory.okResponse(discountCodeService.checkDiscountValid(discountCodeCheck));
//    }

    @ApiOperation(value = "推荐码查询列表")
    @PostMapping("/list")
    public Response<PageVO<DiscountCodeInfoVO>> inviteList(@PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable,
                                                           @RequestBody DiscountCodeForm form) {
        return ResponseFactory.okResponse(discountCodeService.queryDiscountCodeInfos(form, pageable));
    }

    @Log(title = "新增/修改推荐码信息")
    @ApiOperation(value = "新增/修改推荐码信息")
    @PostMapping("/update")
    public Response<Long> updateDiscountCodeInfo(@RequestBody @Validated DiscountCodeVO discountCode) {
        return ResponseFactory.okResponse(discountCodeService.updateDiscountCodeInfo(discountCode));
    }


    @ApiOperation("下载推荐码查询列表信息")
    @PostMapping("/list/download")
    public void downloadDiscountListInfos(HttpServletResponse response) {
        discountCodeService.downloadDiscountListInfos(response);
    }

    @Log(title = "删除推荐码信息")
    @ApiOperation(value = "删除推荐码信息")
    @PostMapping("/remove")
    public Response<Boolean> removeDiscountCodeInfo(@RequestBody List<Long> discountCodeIds) {
        return ResponseFactory.okResponse(discountCodeService.removeDiscountCodeInfo(discountCodeIds));
    }

    @Log(title = "更新推荐码状态")
    @ApiOperation(value = "更新推荐码状态")
    @PostMapping("/change")
    public Response<Boolean> changeDiscountCodeStatus(@RequestBody DiscountCodeStatusForm form) {
        return ResponseFactory.okResponse(discountCodeService.changeDiscountCodeStatus(form));
    }


    @Log(title = "上传推荐码信息")
    @ApiOperation("上传推荐码信息")
    @PostMapping("/upload")
    public void uploadDiscountCodeInfos(@RequestBody MultipartFile file, HttpServletResponse response) {
        discountCodeService.uploadDiscountCodeInfos(file, response);
    }


    @ApiOperation("下载推荐码信息模板")
    @PostMapping("/download")
    public void downloadExcelTemplate(HttpServletResponse response) {
        discountCodeService.downloadExcelTemplate(response);
    }

    @ApiOperation("推荐码使用明细")
    @PostMapping("/use/detail")
    public Response<List<DiscountCodeUseDetailVO>> discountUseDetail(@RequestParam Long discountCodeId) {
        return ResponseFactory.okResponse(discountCodeService.discountUseDetail(discountCodeId));
    }


    @ApiOperation("导出推荐码使用明细")
    @PostMapping("/use/detail/export")
    public void discountUseDetailExport(@RequestParam Long discountCodeId, HttpServletResponse response) {
        discountCodeService.discountUseDetailExport(discountCodeId, response);
    }


}
